package com.qinggeng.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.qinggeng.common.enums.GoodsImgEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.redis.RedisCache;
import com.qinggeng.project.system.domain.GoodsImg;
import com.qinggeng.project.system.dto.GoodsImgDTO;
import com.qinggeng.project.system.mapper.GoodsImgMapper;
import com.qinggeng.project.system.service.GoodsImgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

@Service
public class GoodsImgServiceImpl implements GoodsImgService {
    @Resource
    private GoodsImgMapper goodsImgMapper;


    @Override
    public boolean insertGoodsMainImgs(GoodsImgDTO dto) {
        List<String> image=dto.getGoodsMainImgs();
        dto.setType(GoodsImgEnum.MAIN.getCode());
        for (int i = 0; i <image.size() ; i++) {
            dto.setGoodsImg(image.get(i));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            calendar.add(calendar.DATE,i);
            Date date=calendar.getTime();
            dto.setShowDate(date);
            insert(dto);
        }
        return true;
    }
    private boolean  insert(GoodsImgDTO dto){
        if (null ==dto.getGoodsId()){
            throw  new CustomException("商品id不能为空");
        }
        String img=dto.getGoodsImg();
        if (StringUtils.isEmpty(img)){
            throw  new CustomException("图片不能为空");
        }
        GoodsImg goodsImg =new GoodsImg();
        Date date=new Date();
        goodsImg.setGoodsId(dto.getGoodsId());
        goodsImg.setCreateTime(date);
        goodsImg.setUpdateTime(date);
        goodsImg.setGoodsImg(img);
        Date showDate=dto.getShowDate();
        goodsImg.setShowDate(showDate);
        Integer type =dto.getType();
        GoodsImgEnum.checkCode(type);
        goodsImg.setType(type);
       return goodsImgMapper.insertSelective(goodsImg)>=1;
    }

    @Override
    public boolean insertGoodsDetailsImgs(GoodsImgDTO dto) {
       List<String> imgs= dto.getGoodstDetailsImgs();
       if (null ==imgs||imgs.isEmpty()){
           throw  new CustomException("详情图片不能未空");
        }
        dto.setType(GoodsImgEnum.DETAILS.getCode());
        for (int i = 0; i <imgs.size() ; i++) {
            dto.setGoodsImg(imgs.get(i));
            insert(dto);
        }
        return true;
    }

    @Override
    public boolean insertGoodsLongImgs(GoodsImgDTO dto) {
        List<String> imgs= dto.getGoodstLongImgs();
        if (null ==imgs||imgs.isEmpty()){
            throw  new CustomException("详情长图不能未空");
        }
        dto.setType(GoodsImgEnum.LONG.getCode());
        for (int i = 0; i <imgs.size() ; i++) {
            dto.setGoodsImg(imgs.get(i));
            insert(dto);
        }
        return true;
    }

    @Transactional
    @Override
    public boolean insertGoodsImgs(GoodsImgDTO dto) {
        insertGoodsMainImgs(dto);
        insertGoodsDetailsImgs(dto);
        insertGoodsLongImgs(dto);
        return true;
    }

    @Override
    public String getMainImage(Long goodsId) {

        Example example =new Example(GoodsImg.class);
        example.setOrderByClause(getOrderByMainsSql());
        example.createCriteria()
                .andEqualTo("goodsId", goodsId)
                //.andEqualTo("showDate", DateUtils.getDate())
                .andEqualTo("status",0)
                .andEqualTo("type",GoodsImgEnum.MAIN.getCode())
                .andEqualTo("delFlag",0);
        GoodsImg goodsImg= goodsImgMapper.selectOneByExample(example);
        if (goodsImg ==null){
            return  "";
        }
        return goodsImg.getGoodsImg();
    }

    private  String getOrderBySql(int limit){
        LocalDate localDate=LocalDate.now();
        int day=localDate.getDayOfMonth();
        if (day%2==0){
            return  "id limit "+limit;
        }else {
            return  "id desc limit "+limit;
        }
    }

    private  String getOrderByDetailsSql(){
        return  getOrderBySql(1000);
    }

    private  String getOrderByMainsSql(){
        return  getOrderBySql(1);
    }

    @Override
    public List<String> listDetailsImages(Long goodsId) {
        Example example =new Example(GoodsImg.class);
        example.setOrderByClause(getOrderByDetailsSql());
        example.createCriteria()
                .andEqualTo("goodsId", goodsId)
                //.andEqualTo("showDate", DateUtils.getDate())
                .andEqualTo("status",0)
                .andEqualTo("type",GoodsImgEnum.DETAILS.getCode())
                .andEqualTo("delFlag",0);
        List<GoodsImg> goodsImgs= goodsImgMapper.selectByExample(example);
        List<String> strings=new ArrayList<>();
        if (goodsImgs ==null || goodsImgs.isEmpty()) {
            return strings;
        }
        for (int i = 0; i < goodsImgs.size(); i++) {
            strings.add(goodsImgs.get(i).getGoodsImg());
        }
        return  strings;
    }

    @Override
    public List<String> listLongImages(Long goodsId) {
        Example example =new Example(GoodsImg.class);
        example.setOrderByClause(getOrderByDetailsSql());
        example.createCriteria()
                .andEqualTo("goodsId", goodsId)
                //.andEqualTo("showDate", DateUtils.getDate())
                .andEqualTo("status",0)
                .andEqualTo("type",GoodsImgEnum.LONG.getCode())
                .andEqualTo("delFlag",0);
        List<GoodsImg> goodsImgs= goodsImgMapper.selectByExample(example);
        List<String> strings=new ArrayList<>();
        if (goodsImgs ==null || goodsImgs.isEmpty()) {
            return strings;
        }
        for (int i = 0; i < goodsImgs.size(); i++) {
            strings.add(goodsImgs.get(i).getGoodsImg());
        }
        return  strings;
    }

    @Override
    public Map<String,List<String>> listLongImages(Iterable<String> goodsIds) {
        Map<String,List<String>> map=new HashMap<>();
        Example example =new Example(GoodsImg.class);
        example.setOrderByClause(getOrderByDetailsSql());
        example.createCriteria()
                .andIn("goodsId", goodsIds)
                //.andEqualTo("showDate", DateUtils.getDate())
                .andEqualTo("status",0)
                .andEqualTo("type",GoodsImgEnum.LONG.getCode())
                .andEqualTo("delFlag",0);
        List<GoodsImg> goodsImgs= goodsImgMapper.selectByExample(example);
        if (goodsImgs ==null || goodsImgs.isEmpty()) {
            return map;
        }
        for (String goodsId : goodsIds) {
            List<String> strings=new ArrayList<>();
            for (int i = 0; i < goodsImgs.size(); i++) {
                GoodsImg goodsImg= goodsImgs.get(i);
                if (goodsId.equals(String.valueOf(goodsImg.getGoodsId()))) {
                    strings.add(goodsImg.getGoodsImg());
                }
            }
            map.put(goodsId,strings);
        }
        return  map;
    }

    
    
    @Override
    public String getLongImages(Long goodsId) {
        List<String> strings=listLongImages( goodsId);
        StringBuilder stringBuffer =new StringBuilder();
        for (int i = 0; i <strings.size() ; i++) {
            stringBuffer.append(strings.get(i));
            if (i<(strings.size()-1)){
                stringBuffer.append(",");
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public String getDetailsImages(Long goodsId) {
        List<String> strings=listDetailsImages(goodsId);
        StringBuilder stringBuffer =new StringBuilder();
        for (int i = 0; i <strings.size() ; i++) {
            stringBuffer.append(strings.get(i));
            if (i<(strings.size()-1)){
                stringBuffer.append(",");
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public List<GoodsImg> listGoodsImgNotToDay() {
        Example example =new Example(GoodsImg.class);
        example.createCriteria()
                .andNotEqualTo("showDate", DateUtils.getDate())
                .andEqualTo("status",0)
                .andEqualTo("delFlag",0);
        return goodsImgMapper.selectByExample(example);
    }

    @Override
    public boolean updateGoodsImg(GoodsImg goodsImg) {
        GoodsImg g=new GoodsImg();
        g.setId(goodsImg.getId());
        g.setShowDate(new Date());
        return goodsImgMapper.updateByPrimaryKeySelective( g)>=1;
    }

    @Override
    public String getSysMainImages(Long goodsId) {
        Example example =new Example(GoodsImg.class);
        example.createCriteria()
                .andEqualTo("goodsId", goodsId)
                .andEqualTo("status",0)
                .andEqualTo("delFlag",0)
                .andEqualTo("type",GoodsImgEnum.MAIN.getCode());
       List<GoodsImg> goodsImgs= goodsImgMapper.selectByExample(example);
       StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i <goodsImgs.size() ; i++) {
           GoodsImg goodsImg= goodsImgs.get(i);
            stringBuilder.append(goodsImg.getGoodsImg());
            if (i<(goodsImgs.size()-1)){
                stringBuilder.append(",");
            }
        }
        return stringBuilder.toString();
    }

    @Override
    public String getSysDetailsImages(Long goodsId) {
        Example example =new Example(GoodsImg.class);
        example.createCriteria()
                .andEqualTo("goodsId", goodsId)
                .andEqualTo("status",0)
                .andEqualTo("delFlag",0)
                .andEqualTo("type",GoodsImgEnum.DETAILS.getCode());
        List<GoodsImg> goodsImgs= goodsImgMapper.selectByExample(example);
        StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i <goodsImgs.size() ; i++) {
            GoodsImg goodsImg= goodsImgs.get(i);
            stringBuilder.append(goodsImg.getGoodsImg());
            if (i<(goodsImgs.size()-1)){
                stringBuilder.append(",");
            }
        }
        return stringBuilder.toString();
    }

    @Override
    public String getSysLongImages(Long goodsId) {
        Example example =new Example(GoodsImg.class);
        example.createCriteria()
                .andEqualTo("goodsId", goodsId)
                .andEqualTo("status",0)
                .andEqualTo("delFlag",0)
                .andEqualTo("type",GoodsImgEnum.LONG.getCode());
        List<GoodsImg> goodsImgs= goodsImgMapper.selectByExample(example);
        StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i <goodsImgs.size() ; i++) {
            GoodsImg goodsImg= goodsImgs.get(i);
            stringBuilder.append(goodsImg.getGoodsImg());
            if (i<(goodsImgs.size()-1)){
                stringBuilder.append(",");
            }

        }
        return stringBuilder.toString();
    }

    @Override
    public boolean deleteImages(Long goodsId) {
        Example example =new Example(GoodsImg.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);
        return goodsImgMapper.deleteByExample(example)>=1;
    }
}
